

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import be.ipl.albums.domaine.Album;
import be.ipl.albums.domaine.Album.Categorie;
import be.ipl.albums.exceptions.AlbumExistantException;
import be.ipl.albums.usecases.GestionAlbums;

public class TestNG_albums1 {
	private static GestionAlbums gestionAlbums;

	private static String[] nomsAlbums = { "Come Around Sundown", "North",
			"Sale el Sol", "An Introduction to Syd Barrett",
			"Clinic For Dolls", "Close-Up Vol. 2, People and Places",
			"Zeroes QC" };
	private static String[] artistes = { "Kings of Leon", "Darkstar",
			"Shakira", "Syd Barrett", "UnSun", "Suzanne Vega", "Suuns" };
	private static Calendar[] datesDAjout = {
			new GregorianCalendar(2010, Calendar.OCTOBER, 2),
			new GregorianCalendar(2010, Calendar.OCTOBER, 14),
			new GregorianCalendar(2010, Calendar.OCTOBER, 13),
			new GregorianCalendar(2010, Calendar.OCTOBER, 13), 
			new GregorianCalendar(2010, Calendar.OCTOBER, 13), 
			new GregorianCalendar(2010, Calendar.OCTOBER, 12), 
			new GregorianCalendar(2010, Calendar.OCTOBER, 12)
	};
	private static Categorie[] categories = { Categorie.ROCK,
		Categorie.ELECTRONIC, Categorie.POP, Categorie.ROCK,
		Categorie.METAL, Categorie.FOLK, Categorie.INDIE };
	private static String[] styles = { "Alt Rock", "Electronic", "Pop",
			"Psychedelic Rock", "Gothic Metal", "Pop Folk", "Shoegaze" };
	private static int[] debits = { 169, 320, 320, 256, 320, 168, 227 };
	private static double[] tailles = { 61, 90, 116, 144, 99, 51, 62 };

	private static Album[] albums;
	
	@BeforeClass
	public static void init() throws NamingException {
		Context jndi = new InitialContext();
		gestionAlbums = (GestionAlbums) jndi.lookup("ejb:/eAlbums1_EJB/GestionAlbumsImpl!be.ipl.albums.usecases.GestionAlbums");
		albums = new Album[nomsAlbums.length];
		for (int i = 0; i < nomsAlbums.length; i++) {
			albums[i] = new Album(nomsAlbums[i], artistes[i], datesDAjout[i],
					categories[i], styles[i], debits[i], tailles[i]);
			try {
				gestionAlbums.ajouter(albums[i]);
			} catch (AlbumExistantException e) {
				throw new InternalError();
			}
		}
	}
	
	@Test(groups="0")
	public void testListeOrdonnee() {
		List<Album> liste = gestionAlbums.listeOrdonnee();
		for (int i = 0; i < 4; i++) {
			assertEquals(nomsAlbums[i+1], liste.get(i).getNom());
			assertEquals(artistes[i+1], liste.get(i).getArtiste());
			assertEquals(datesDAjout[i+1], liste.get(i).getDateDAjout());
			assertEquals(categories[i+1], liste.get(i).getCategorie());
			assertEquals(styles[i+1], liste.get(i).getStyle());
			assertEquals(debits[i+1], liste.get(i).getDebit());
			assertEquals(tailles[i+1], liste.get(i).getTaille(), 0);
			albums[i + 1] = liste.get(i); 
		}
		assertEquals(nomsAlbums[5], liste.get(5).getNom());
		assertEquals(artistes[5], liste.get(5).getArtiste());
		assertEquals(datesDAjout[5], liste.get(5).getDateDAjout());
		assertEquals(categories[5], liste.get(5).getCategorie());
		assertEquals(styles[5], liste.get(5).getStyle());
		assertEquals(debits[5], liste.get(5).getDebit());
		assertEquals(tailles[5], liste.get(5).getTaille(), 0);
		albums[5] = liste.get(5); 

		assertEquals(nomsAlbums[6], liste.get(4).getNom());
		assertEquals(artistes[6], liste.get(4).getArtiste());
		assertEquals(datesDAjout[6], liste.get(4).getDateDAjout());
		assertEquals(categories[6], liste.get(4).getCategorie());
		assertEquals(styles[6], liste.get(4).getStyle());
		assertEquals(debits[6], liste.get(4).getDebit());
		assertEquals(tailles[6], liste.get(4).getTaille(), 0);
		albums[6] = liste.get(4); 

		assertEquals(nomsAlbums[0], liste.get(6).getNom());
		assertEquals(artistes[0], liste.get(6).getArtiste());
		assertEquals(datesDAjout[0], liste.get(6).getDateDAjout());
		assertEquals(categories[0], liste.get(6).getCategorie());
		assertEquals(styles[0], liste.get(6).getStyle());
		assertEquals(debits[0], liste.get(6).getDebit());
		assertEquals(tailles[0], liste.get(6).getTaille(), 0);
		albums[0] = liste.get(6); 
	}
	
	@Test(groups="1", dependsOnGroups="0")
	public void testListerCategorie() {
		List<Album> liste2 = gestionAlbums.lister(Categorie.ROCK);
		assertEquals(2, liste2.size());
		assertEquals(albums[3], liste2.get(0));
		assertEquals(albums[0], liste2.get(1));	
	}
	
	@Test(groups="1", dependsOnGroups="0")
	public void testListerDepuis() {
		List<Album> liste3 = gestionAlbums.listerDepuis(new GregorianCalendar(2010, Calendar.OCTOBER, 13));
		assertEquals(4, liste3.size());
		for (int i = 0; i < 4; i++) {
			assertEquals(albums[i+1], liste3.get(i));
		}
	}
	
	@Test(groups="1", dependsOnGroups="0")
	public void testListeDuJour() {
		List<Album> liste4 = gestionAlbums.listeDuJour();
		assertEquals(0, liste4.size());
	}
	
	@Test(groups="1", dependsOnGroups="0")
	public void testListeMots1() {
		List<Album> liste5 = gestionAlbums.lister("eo oe");
		assertEquals(3, liste5.size());
		System.out.println("================ eo oe =================");
		assertEquals(albums[6], liste5.get(0));
		System.out.println(liste5.get(0).getArtiste());
		assertEquals(albums[5], liste5.get(1));
		System.out.println(liste5.get(1).getArtiste());
		assertEquals(albums[0], liste5.get(2));
		System.out.println(liste5.get(2).getArtiste());
		System.out.println("========================================");
	}

	@Test(groups="1", dependsOnGroups="0")
	public void testListeMots2() {
		List<Album> liste6 = gestionAlbums.lister("indie");
		assertEquals(1, liste6.size());
		System.out.println("================ indie =================");
		assertEquals(albums[6], liste6.get(0));
		System.out.println(liste6.get(0).getArtiste());
		System.out.println("========================================");
	}

	@Test(dependsOnGroups="1")
	public void testAjouter() {
		Album album = new Album("Come Around Sundown", "Kings of Leon",
				new GregorianCalendar(), // mettre la date du jour
				Categorie.ROCK, "Alt Rock", 320, 108);
		try {
			gestionAlbums.ajouter(album);
			assertTrue(true);
		} catch (AlbumExistantException e) {
			throw new InternalError();
		}
		List<Album> liste = gestionAlbums.listeOrdonnee();
			assertEquals(nomsAlbums[0], liste.get(0).getNom());
			assertEquals(artistes[0], liste.get(0).getArtiste());
			albums[0] = liste.get(0);
			for (int i = 0; i < 5; i++) {
				assertEquals(albums[i], liste.get(i));
			}
			assertEquals(albums[6], liste.get(5));
			assertEquals(albums[5], liste.get(6));
	}

	@Test(dependsOnMethods="testAjouter")
	public void testListeDuJour2() {
		List<Album> liste4 = gestionAlbums.listeDuJour();
		assertEquals(1, liste4.size());
		assertEquals(albums[0], liste4.get(0));
	}
}
